//
//  HomeNewsCell.m
//  GuShiNews
//
//  Created by huachun on 2019/8/16.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "HomeNewsCell.h"
#import "NewsModel.h"

@interface HomeNewsCell ()

@property (nonatomic, strong) UILabel *Label_tittle;
@property (nonatomic, strong) UILabel *Label_bottom;
@property (nonatomic, strong) UILabel *Label_info;
@property (nonatomic, strong) UIImageView *News_image;

@property (nonatomic, strong) CALayer *News_line;

@end

@implementation HomeNewsCell

+ (instancetype)cellForTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"HomeNewsCell";
    
    HomeNewsCell  *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell)
    {
        cell = [[HomeNewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        cell.backgroundColor          = [UIColor whiteColor];
        
        [cell.contentView addSubview:cell.Label_tittle];
        [cell.contentView addSubview:cell.Label_bottom];
        [cell.contentView addSubview:cell.Label_info];
        [cell.contentView addSubview:cell.News_image];
        [cell.contentView.layer addSublayer:cell.News_line];
        
    }
    
    return cell;
}

- (void)setModel:(NewsModel *)Model{
    if (_Model != Model) {
        _Model = Model;
    }
    
    NSDictionary *dic = @{NSKernAttributeName:@0.f};
    NSMutableAttributedString * attributedString =     [[NSMutableAttributedString alloc] initWithString:_Model.news_tittle attributes:dic];
    
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc]     init];
    [paragraphStyle setLineSpacing:4];
    
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [_Model.news_tittle length])];
    [_Label_tittle setAttributedText:attributedString];
    [_Label_tittle sizeToFit];
    
    _News_line.frame=CGRectMake(20, _Model.news_height-1, RECRWITCH-40, 0.7);
    
    if (_Model.news_show_type==0) {
        _News_image.hidden = YES;
        
        _Label_tittle.frame=CGRectMake(20, 20, RECRWITCH-40, 50);
        _Label_info.frame = CGRectMake(20, 75, _Label_tittle.frame.size.width, 20);
    }else{
        _News_image.hidden = NO;
        [_News_image sd_setImageWithURL:[NSURL URLWithString:_Model.news_img]];
        
        _Label_tittle.frame=CGRectMake(20, 20, RECRWITCH/3*2-40, 50);
        _Label_info.frame = CGRectMake(20, 75, RECRWITCH/3*2-40, 20);
        _News_image.frame=CGRectMake(RECRWITCH/3*2, 20, RECRWITCH/3-20, 80);
        if (_Model.news_show_type==2) {
            _Label_tittle.frame=CGRectMake(20, 20, RECRWITCH-40, 50);
            _Label_info.frame = CGRectMake(20, 75, _Label_tittle.frame.size.width, 20);
            _News_image.frame=CGRectMake(20, 135, RECRWITCH-40, (RECRWITCH-40)*0.618);
        }
    }
    
    NSString *bottom_string;
    if ([_Model.news_count intValue]>0) {
        bottom_string = [NSString stringWithFormat:@"%@评论  %@", _Model.news_count,[JonesClass timeZhuan:_Model.news_date]];
    }else{
        bottom_string = [NSString stringWithFormat:@"%@",[JonesClass timeZhuan:_Model.news_date]];
    }
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:bottom_string];
//    [str addAttribute:NSForegroundColorAttributeName value:[JonesClass stringTOColor:@"#666666"] range:NSMakeRange(0,_Model.news_come.length+1)];
    _Label_bottom.attributedText = str;
    
    
    if (_Model.IsRead) {
        _Label_tittle.textColor = [JonesClass stringTOColor:@"#888888"];
    }else{
        _Label_tittle.textColor = NewsTitleColor;
    }
    
    NSString *lincoutn = [NSString stringWithFormat:@"%ld", (long)_Model.wen_count];
    
    if (_Model.wen_count>1000) {
        _Model.wen_time = [lincoutn floatValue]/750.00f;
    }else if (_Model.wen_count>2000){
        _Model.wen_time = [lincoutn floatValue]/850.00f;
    }
    else{
        _Model.wen_time = [lincoutn floatValue]/700.00f;
    }
    
    if (_Model.wen_time>1) {
        _Label_info.text=[NSString stringWithFormat:@"阅读约%.0f分钟·%@",_Model.wen_time,_Model.wen_string];
    }else{
        _Label_info.text=[NSString stringWithFormat:@"阅读约%.0f秒·%@",_Model.wen_time*60,_Model.wen_string];
    }
}

-(UILabel *)Label_tittle{
    if (!_Label_tittle) {
        _Label_tittle           = [[UILabel alloc] init];
        _Label_tittle.font      = [UIFont boldSystemFontOfSize:17];
        _Label_tittle.textColor = NewsTitleColor;
        _Label_tittle.numberOfLines = 2;
        _Label_tittle.frame=CGRectMake(20, 20, RECRWITCH/3*2-40, 50);
//        _Label_tittle.lineBreakMode = NSLineBreakByCharWrapping;
    }
    return _Label_tittle;
}

-(UILabel *)Label_info{
    if (!_Label_info) {
        _Label_info           = [[UILabel alloc] init];
        _Label_info.font      = [UIFont systemFontOfSize:14];
        _Label_info.textColor = [JonesClass stringTOColor:@"#666666"];
        _Label_info.frame = CGRectMake(20, 75, _Label_tittle.frame.size.width, 20);
    }
    return _Label_info;
}

-(UILabel *)Label_bottom{
    if (!_Label_bottom) {
        _Label_bottom           = [[UILabel alloc] init];
        _Label_bottom.font      = [UIFont systemFontOfSize:13];
        _Label_bottom.textColor = NewsotherColor;
        _Label_bottom.frame = CGRectMake(20, 105, _Label_tittle.frame.size.width, 20);
    }
    return _Label_bottom;
}

-(UIImageView *)News_image{
    if (!_News_image) {
        _News_image                     = [[UIImageView alloc] init];
        _News_image.layer.masksToBounds = YES;
        _News_image.layer.cornerRadius  = 5;
        _News_image.contentMode         = UIViewContentModeScaleAspectFill;
        _News_image.backgroundColor = backColor;
        _News_image.frame=CGRectMake(RECRWITCH/3*2, 20, RECRWITCH/3-20, 80);
    }
    return _News_image;
}

- (CALayer *)News_line{
    if (!_News_line) {
        _News_line = [[CALayer alloc] init];
        _News_line.frame=CGRectMake(20, 119, RECRWITCH-40, 0.7);
        _News_line.backgroundColor=LineColor.CGColor;
    }
    return _News_line;
}

@end
